package com.njwb.day15.zqzb.ios.io;

import java.io.*;
import java.util.Scanner;

public class File {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入宠物名字：");
        String name= sc.next();
        System.out.println("请输入宠物种类：");
        String type= sc.next();
        System.out.println("请输入宠物主人名字：");
        String masterName= sc.next();
        Pet pet=new Pet();
        pet.setName(name);
        pet.setType(type);
        pet.setMasterName(masterName);


        BufferedReader bfr=null;
        BufferedWriter bfw=null;
        OutputStreamWriter otw=null;
        try {
            bfr = new BufferedReader(new FileReader("src/com/njwb/io/pet.template"));
            bfw=new BufferedWriter(new FileWriter("src/com/njwb/io/dog.txt"));
            String line;
            while ((line = bfr.readLine()) != null) {
                // 替换模板占位符
                line = line.replace("{name}", pet.getName())
                        .replace("{type}", pet.getType())
                        .replace("{master}", pet.getMasterName());
                bfw.write(line);
                bfw.newLine(); // 保持换行
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            try {
                if (bfr != null) bfr.close();
                if (bfw != null) bfw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
